package com.ryder.petmatediarybackend.module.pet.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.pet.dto.ApplyTagDTO;
import com.ryder.petmatediarybackend.module.pet.entity.PetTagApplication;
import com.ryder.petmatediarybackend.module.pet.service.PetTagApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 描述：宠物标签申请 Controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-10-30 21:30:00
 */
@Slf4j
@Tag(name = "宠物标签申请管理", description = "用户申请新标签相关接口（需要登录）")
@RestController
@RequestMapping("/pet/tag-application")
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class PetTagApplicationController {

    private final PetTagApplicationService tagApplicationService;

    @Operation(summary = "申请新标签", description = "用户申请添加新的宠物标签（5分钟内只能申请一次）")
    @PostMapping("/apply")
    public Result<PetTagApplication> applyNewTag(@Valid @RequestBody ApplyTagDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[标签申请] 用户ID={} 执行申请新标签, 方法=applyNewTag, 标签名称={}", userId, dto.getTagName());
        
        PetTagApplication application = tagApplicationService.applyNewTag(userId, dto);
        
        log.info("[标签申请] 用户ID={} 申请新标签成功, 申请ID={}", userId, application.getId());
        return Result.ok(application);
    }

    @Operation(summary = "检查是否可以申请", description = "检查用户是否可以申请新标签（5分钟限制）")
    @GetMapping("/can-apply")
    public Result<Boolean> canApply() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[标签申请] 用户ID={} 执行检查申请权限, 方法=canApply", userId);
        
        boolean canApply = tagApplicationService.canApply(userId);
        
        log.info("[标签申请] 用户ID={} 检查申请权限, 结果={}", userId, canApply);
        return Result.ok(canApply);
    }
}
